//
//  OrderDetailsViewController.m
//  Edumap
//
//  Created by 辰领科技 on 16/7/18.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "OrderDetailsViewController.h"
#import "PayTypeViewController.h"
#import "WriteCommentViewController.h"

@interface OrderDetailsViewController ()
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;//联系人
@property (strong, nonatomic) IBOutlet UILabel *phoneNumberLabel;//手机号
@property (strong, nonatomic) IBOutlet UILabel *addressLabel;//地址
@property (strong, nonatomic) IBOutlet UILabel *orderNumberLabel;//订单编号
@property (strong, nonatomic) IBOutlet UILabel *typeLabel;//付款方式
@property (strong, nonatomic) IBOutlet UILabel *timeLabel;//支付时间
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *moneyLabel;
@property (strong, nonatomic) IBOutlet UILabel *timesLabel;
@property (strong, nonatomic) IBOutlet UILabel *detailLabel;
@property (strong, nonatomic) IBOutlet UILabel *totalAmountLabel;//总金额

@property (strong, nonatomic) NSDictionary *dic;
@property (weak, nonatomic) IBOutlet UIButton *callButton;
@property (weak, nonatomic) IBOutlet UIButton *payButton;

@end

@implementation OrderDetailsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
    if ([_ChooseInterface isEqualToString:@"1"]) {
        [self TutorOrderDetails];
    } else if ([_ChooseInterface isEqualToString:@"2"]) {
        [self CourseOrderDetails];
    }

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadPay) name:@"hadPay" object:@"1"];

}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    _scroview.contentSize = flexibleSize(CGSizeMake(0, 466), NO);
    self.view.backgroundColor = [UIColor colorWithWhite:0.949 alpha:1.000];
}

#pragma mark == 支付成功
- (void)hadPay{
    [self backBtn];
}


- (void)updateLayout {
    if ([_ChooseInterface isEqualToString:@"1"]) {
        _nameLabel.text = [NSString stringWithFormat:@"%@",_dic[@"msadReceiverName"]];
        _phoneNumberLabel.text = [NSString stringWithFormat:@"%@",_dic[@"msadMobileNo"]];
        _addressLabel.numberOfLines = 0;
        _addressLabel.text = [NSString stringWithFormat:@"%@",_dic[@"msadAddr"]];
        [_addressLabel sizeToFit];
        _orderNumberLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mlorNo"]];
        if ([_dic[@"paymentType"] isEqual:[NSNull null]]) {
            _typeLabel.text = @"";
        }else{
            _typeLabel.text = [NSString stringWithFormat:@"%@",_dic[@"paymentType"]];
        }
        _timeLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mlorPayTime"]];
        
        NSString * image = [NSString stringWithFormat:@"%@",_dic[@"mallImage"]];
        if (image.length>0) {
            [_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
        }
        _titleLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mstoName"]];
        _moneyLabel.text = [NSString stringWithFormat:@"￥%@/小时",_dic[@"mallprice"]];
        
        NSString *mlsuNames = @"";
        NSArray *array = _dic[@"mlss"];
        for (int i = 0; i < array.count; i++) {
            NSString *mlsuName = [NSString stringWithFormat:@"%@",array[i][@"mlsuName"]];
            mlsuNames = [mlsuNames stringByAppendingString:mlsuName];
        }
        _detailLabel.text = mlsuNames;
        _totalAmountLabel.text = [NSString stringWithFormat:@"￥%@",_dic[@"mallprice"]];
        
        
        if ([_dic[@"mlorStatus"]integerValue] == 1){
            _typeLabel.text = @"未支付";
        }else if ([_dic[@"mlorStatus"]integerValue] == 2 ) {
            [_payButton setTitle:@"评论" forState:UIControlStateNormal];
        }else if ([_dic[@"mlorStatus"]integerValue] == 3){
            [_payButton setTitle:@"已评论" forState:UIControlStateNormal];
        }else if ([_dic[@"mlorStatus"]integerValue] == 4){
            [_payButton setTitle:@"已取消" forState:UIControlStateNormal];
        }
      

    } else if ([_ChooseInterface isEqualToString:@"2"]){
        _nameLabel.text = [NSString stringWithFormat:@"%@",_dic[@"msadReceiverName"]];
        _phoneNumberLabel.text = [NSString stringWithFormat:@"%@",_dic[@"msadMobileNo"]];
        
        _addressLabel.numberOfLines = 0;
        _addressLabel.text = [NSString stringWithFormat:@"%@",_dic[@"msadAddr"]];
        [_addressLabel sizeToFit];
        
        _orderNumberLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mordNo"]];
        _typeLabel.text = [NSString stringWithFormat:@"%@",_dic[@"paymentType"]];
        _timeLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mordPayTime"]];
        
        NSString * image = [NSString stringWithFormat:@"%@",_dic[@"mallImage"]];
        if (image.length>0) {
            [_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
        }
        _titleLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mstoName"]];
        _moneyLabel.text = [NSString stringWithFormat:@"￥%@/节",_dic[@"mallprice"]];
//        _timesLabel.text = [NSString stringWithFormat:@"x%@",_dic[@"mallOrderDetail"][@"modeNum"]];
        _timesLabel.text = @"x1";
        
        _totalAmountLabel.text = [NSString stringWithFormat:@"￥%ld",[_dic[@"mallprice"]integerValue]];
        
        _timeLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mordPayTime"]];
    
        _detailLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mallContent"]];
        
        _totalAmountLabel.text = [NSString stringWithFormat:@"%@",_dic[@"mallprice"]];
        
        
        if ([_dic[@"mlorStatus"]integerValue] == 1){
            _typeLabel.text = @"未支付";
        }else if ([_dic[@"mordStatus"]integerValue] == 2 ) {
            [_payButton setTitle:@"评论" forState:UIControlStateNormal];
        }else if ([_dic[@"mordStatus"]integerValue] == 3){
            [_payButton setTitle:@"已评论" forState:UIControlStateNormal];
        }else if ([_dic[@"mordStatus"]integerValue] == 4){
            [_payButton setTitle:@"已取消" forState:UIControlStateNormal];
        }
    }
    
}
- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark - 拨打电话
- (IBAction)phone:(id)sender {
    [Common callPhone:_phoneNumberLabel.text view:self.view];
}
#pragma mark - 立即支付
- (IBAction)pay:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"立即支付"]) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
        PayTypeViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PayType"];
        page.payOrderPrice = [_totalAmountLabel.text substringFromIndex:2];
        page.payOrderName = _titleLabel.text;
        if ([_ChooseInterface isEqualToString:@"1"]) {
            page.payId = _mlorId;
            page.payOrderNo = [NSString stringWithFormat:@"%@",_dic[@"mlorNo"]];
        } else if ([_ChooseInterface isEqualToString:@"2"]) {
            page.payId = _mordId;
            page.payOrderNo = [NSString stringWithFormat:@"%@",_dic[@"mordNo"]];
        }
        page.payTypes = @"1";
        [[Common getNavigation]pushViewController:page];

    }else if([sender.titleLabel.text isEqualToString:@"评论"]){//评论
        if ([_ChooseInterface isEqualToString:@"1"]) {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
            WriteCommentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WriteComment"];
            page.indentId = _mlorId;
            page.classId = _classId;
            page.type = @"1";
            page.image = [NSString stringWithFormat:@"%@",_dic[@"mallImage"]];
            [[Common getNavigation]pushViewController:page];
           
        }else{
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
            WriteCommentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WriteComment"];
            page.indentId = _mordId;
            page.classId = _classId;
            page.type = @"2";
            page.image = [NSString stringWithFormat:@"%@",_dic[@"mallImage"]];
            [[Common getNavigation]pushViewController:page];
        }
    }
    
    
}


#pragma mark - 获取家教订单数据
- (void)TutorOrderDetails {
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"memberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:_mlorId forKey:@"mlorId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_mallLectureOrderDetails withHttpType:@"POST" withTag:JY_JY_mallLectureOrderDetails];
    
}
#pragma mark - 获取课程订单数据
- (void)CourseOrderDetails {
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"memberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:_mordId forKey:@"mordId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_mallOrderDetails withHttpType:@"POST" withTag:JY_JY_mallOrderDetails];
    
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{

    if (tag==JY_JY_mallLectureOrderDetails) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            _dic = [NSDictionary dictionary];
            _dic = database[@"result"];
            [self updateLayout];
        }
    } else if (tag==JY_JY_mallOrderDetails) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            _dic = [NSDictionary dictionary];
            _dic = database[@"result"];
            [self updateLayout];
        }
    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{

    [self dismissWaitDialog];
}


@end
